#include "tk_debug.h"
#include "tk_define.h"
#include "tk_mutex.h"

#include <QFile>
#include <QTextStream>
#include <QDateTime>

using namespace tk;

static TkMutex sz_debug_mutex;
//===================================================================================

void DebugMsgPrint(QtMsgType &type, const QMessageLogContext &context,const QString &msg)
{
	Q_UNUSED(context);
    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
#ifdef DEBUG
	case QtDebugMsg:
		fprintf(stderr, "Debug: %s (%s:%u, %s)\n",
			localMsg.constData(), context.file, context.line, context.function);
		break;
	case QtInfoMsg:
		fprintf(stderr, "Info: %s (%s:%u, %s)\n",
			localMsg.constData(), context.file, context.line, context.function);
		break;
	case QtWarningMsg:
		fprintf(stderr, "Warning: %s (%s:%u, %s)\n",
			localMsg.constData(), context.file, context.line, context.function);
		break;
	case QtCriticalMsg:
		fprintf(stderr, "Critical: %s (%s:%u, %s)\n",
			localMsg.constData(), context.file, context.line, context.function);
		break;
	case QtFatalMsg:
		fprintf(stderr, "Fatal: %s (%s:%u, %s)\n",
			localMsg.constData(), context.file, context.line, context.function);
#else
	case QtDebugMsg:
		fprintf(stderr, "Debug: %s\n",
			localMsg.constData());
		break;
	case QtInfoMsg:
		fprintf(stderr, "Info: %s\n",
			localMsg.constData());
		break;
	case QtWarningMsg:
		fprintf(stderr, "Warning: %s\n",
			localMsg.constData());
		break;
	case QtCriticalMsg:
		fprintf(stderr, "Critical: %s\n",
			localMsg.constData());
		break;
	case QtFatalMsg:
		fprintf(stderr, "Fatal: %s\n",
			localMsg.constData());
#endif // DEBUG 
        abort();
    }
}

void DebugMsgLogFile(QtMsgType &type, const QMessageLogContext &context,const QString &msg)
{
	Q_UNUSED(context);
    QByteArray localMsg = msg.toLocal8Bit();

    QString strMsg;
    switch(type)
    {
    case QtDebugMsg:
        strMsg = QString("Debug:");
#ifdef TK_RELEASE_MODE
		return;
#endif // TK_RELEASE_MODE
        break;
    case QtInfoMsg:
        strMsg = QString("Info:");
        break;
    case QtWarningMsg:
        strMsg = QString("Warning:");
        break;
    case QtCriticalMsg:
        strMsg = QString("Critical:");
        break;
    case QtFatalMsg:
        strMsg = QString("Fatal:");
        break;
    }

#ifdef DEBUG
    QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString strMessage =
            QString("[%1] Message:%2 File:%3  Line:%4  Function:%5")
            .arg(strDateTime)
            .arg(localMsg.constData())
            .arg(context.file)
            .arg(context.line)
            .arg(context.function);

#else
	QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
	QString strMessage =
		QString("[%1] Message:%2")
		.arg(strDateTime)
		.arg(localMsg.constData());
#endif 
    QFile file("tk_log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);

    QTextStream stream(&file);
    stream << strMessage << "\r\n";

    file.flush();
    file.close();
}


void TkMessageHandler(QtMsgType type, const QMessageLogContext &context,const QString &msg)
{
    TkMutexLocker locker(sz_debug_mutex);

#ifdef TK_DEBUG_PRINT
    DebugMsgPrint(type,context,msg);
#endif

#ifdef TK_DEBUG_LOG_FILE
    DebugMsgLogFile(type,context,msg);
#endif
}

